package com.itheima.dao.impl;

import com.itheima.dao.IStudentDao;
import com.itheima.domain.Student;
import com.itheima.factory.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;

public class StudentDaoImpl implements IStudentDao {
    private static final Logger log= LoggerFactory.getLogger(StudentDaoImpl.class);

    private static ArrayList<Student> list = new ArrayList<>();

    static {
        Student cai = new Student("0001", "蔡徐坤", 25, "1996");
        Student lu = new Student("0002", "鹿晗", 30, "1996");
        Student huang = new Student("0003", "黄子韬", 28, "1996");
        Student wu = new Student("0004", "吴亦凡", 28, "1996");
        list.add(cai);
        list.add(lu);
        list.add(huang);
        list.add(wu);

    }
    @Override
    public void add(Student student) {
            list.add(student);
            log.debug("完成了新增"+student.toString());

    }

    @Override
    public void delete(String sid) {
        int index = getIndex(sid);
        list.remove(index);
    }

    @Override
    public void update(String sid, Student student) {
        int index = getIndex(sid);
        list.set(index,student);
    }

    @Override
    public ArrayList<Student> findAll() {
        return list;
    }

    @Override
    public int getIndex(String sid) {
        if(list==null||list.size()==0){
            return -1;
        }
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if(student.getSid().equals(sid)){
                return i;
            }
        }

        return -1;
    }

    //根据学号找对应学生对象
    public Object getStudent(String sid) {
        if(list==null||list.size()==0){
            return null;
        }
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if(student.getSid().equals(sid)){
                return student;
            }
        }

        return null;
    }
}
